<template>
    <div class=''>
        <!-- 面包屑导航区域 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统设置</el-breadcrumb-item>
            <el-breadcrumb-item>菜单列表</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 卡片视图区域 -->
        <el-card>
              <el-row :gutter="20">
                <el-col :span="7">
                    <el-input placeholder="请输入内容"  clearable @clear="getMenuList">
                        <el-button slot="append" icon="el-icon-search" @click="getMenuList"></el-button>
                    </el-input>
                </el-col>
                <el-col :span="4">
                    <el-button type="primary" @click="addDialogVisible = true">添加菜单项</el-button>
                </el-col>
              </el-row>

              <el-table
                :data="menuList"
                border
                style="font-size: 14px"  
                v-loading="loading"                           
                >
                <el-table-column
                  fixed
                  type="index"
                  width="80">
                </el-table-column>

                <el-table-column
                  prop="name"
                  label="菜单名"
                  width="200">
                </el-table-column>

                <el-table-column
                  prop="icon"
                  label="图标"
                  width="200">
                </el-table-column>

                <el-table-column
                  prop="perms"
                  label="参数"
                  width="240">
                </el-table-column>

                <el-table-column
                  prop="url"
                  label="地址">
                </el-table-column>

              <el-table-column
                  label="等级"
                  width="150">
                    <template slot-scope="scope">
                        <el-tag type="success" v-if="scope.row.parentId === 0 " size="mini">一级菜单</el-tag>
                        <el-tag type="danger" v-else size="mini">二级菜单</el-tag>
                    </template>
                </el-table-column>

                <el-table-column
                  label="状态"
                  width="150">
                    <template slot-scope="scope">
                        <el-switch v-model="status" @change="menuStateChanged(scope.row)"></el-switch>
                    </template>
                </el-table-column>

                <el-table-column
                  fixed="right"
                  label="操作"
                  width="180">
                  <template slot-scope="scope" >
                     <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog(scope.row)"></el-button>
                     <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteMenu(scope.row)"></el-button>
                     <el-tooltip effect="dark" content="分配权限" placement="top" :enterable="false">
                        <el-button type="warning" icon="el-icon-setting" size="mini" @click="setRole(scope.row)"></el-button>
                    </el-tooltip>
                  </template>                  
                </el-table-column>
              </el-table>           

        </el-card>

        <!-- 添加用户的对话框 -->
        <el-dialog title="添加菜单" :visible.sync="addDialogVisible" width="50%" @close="addDialogClosed" :close-on-click-modal="false" :close-on-press-escape="false">

           <el-form :model="addMenu" :rules="addMenuRules" ref="addMenuRef" label-width="70px">
                <el-form-item label="菜单名" prop="name">
                    <el-input v-model="addMenu.name"></el-input>
                </el-form-item>
                <el-form-item label="图标" prop="icon">
                    <el-input v-model="addMenu.icon" type="password" ></el-input>
                </el-form-item>
                <el-form-item label="参数" prop="perms">
                    <el-input v-model="addMenu.perms"></el-input>
                </el-form-item>
                <el-form-item label="地址" prop="url">
                    <el-input v-model="addMenu.url"></el-input>
                </el-form-item>
           </el-form>

            <!-- 底部区域 -->
            <span slot="footer" class="dialog-footer">
                <el-button @click="addDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="addMenuClick">确 定</el-button>
            </span>
        </el-dialog>

        <!-- 修改用户的对话框 -->
        <el-dialog title="修改菜单信息" :visible.sync="editDialogVisible" @close="editDialogClosed" width="50%" :close-on-click-modal="false" :close-on-press-escape="false">
           <el-form :model="editMenu" :rules="addMenuRules" ref="editMenuRef" label-width="70px">
                <el-form-item label="菜单名">
                     <el-input v-model="editMenu.name" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="图标" prop="icon">
                     <el-input v-model="editMenu.icon" ></el-input>
                </el-form-item>
                <el-form-item label="参数">
                     <el-input v-model="editMenu.perms" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="地址">
                     <el-input v-model="editMenu.url"></el-input>
                </el-form-item>
           </el-form>
            <!-- 底部区域 -->
            <span slot="footer" class="dialog-footer">
                <el-button @click="editDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="editMenuClick">确 定</el-button>
            </span>
        </el-dialog>
        

    </div>
</template>
<script>
export default {
    inject: ['reload'],
    name: '',
    data () {
       
        return {
            loading:false,
            // 菜单数据信息
            menuList: [],
            status: true,
            addDialogVisible: false,
            editDialogVisible: false,          
            addMenu: {
                name: '',
                icon: '',
                perms: '',
                url: ''
            },
            editMenu: { },
            addMenuRules: {
                name: [
                    { required: true, message: '请输入菜单名', trigger: 'blur' }                   
                 ],
                icon: [
                    { required: true, message: '请选择图标', trigger: 'blur' }
                 ],
                perms: [
                    { required: true, message: '请输入参数', trigger: 'blur' }
                 ],
                url: [
                    { required: true, message: '请输入地址', trigger: 'blur' }
                 ]
            }   
        }
    },
    watch: {
    },
    created () {
        this.getMenuList()
    },
    methods: {
        // 获取菜单列表
        async getMenuList () {   
            this.loading = true         
           await this.$http.get('/sys/menu/listAll').then(response=>{
                const res = response.data
                this.loading = false
                if (res.code !== 2000) {
                    return this.$message.error(res.msg)
                }
                console.log(res)
                this.menuList = res.icons
           }).catch(error=>{
               this.$message.error(error)
               this.loading = false
           })               
        },
        // 菜单状态改变
        async menuStateChanged (menuInfo) {
            console.log(menuInfo)           
        },
        // 编辑菜单
        async showEditDialog (menuItem) {           
            this.editMenu = menuItem
            console.log(menuItem)
            this.editDialogVisible = true
        },
        // 删除菜单
        deleteMenu (usreinfo) {
                this.$confirm(`确认删除"${usreinfo.Menuname}"用户信息, 是否继续?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(async () => {
                const { data: res } = await this.$http.delete(`Menus/${usreinfo.id}`)
                if (res.meta.status !== 200) return this.$message.error('用户删除失败!')
                this.getMenuList()
                this.$message.success('用户删除成功!')
            }).catch(() => {
            this.$message.info('已取消删除!')
        })
        },        
        // 监听添加菜单对话框的关闭事件
        addDialogClosed () {
            this.$refs.addMenuRef.resetFields()
        },
        // 添加菜单项
        addMenuClick () {
           this.$message.success('该功能暂未实现!')
        },
        // 监听编辑菜单对话框的关闭事件
        editDialogClosed () {
            this.$refs.editMenuRef.resetFields()
        },
        // 编辑菜单项
        editMenuClick () {
            this.$refs.editMenuRef.validate(async valid => {
                if (!valid) return
                await this.$http.put(`/sys/menu/update`, this.editMenu).then(response=>{
                    const res = response.data
                    console.log(res)
                    if (res.code !== 2000){
                        return this.$message.error(res.msg)
                    } 
                    this.editDialogVisible = false
                    this.getMenuList()
                    this.$message.success('菜单项修改成功!')
                })                
            })
        }

    }
}
</script>

<style scoped>

</style>
